
#define scale @"<html><head><meta name=\"viewport\" content=\"width=device-width\"/><style>pre,code { white-space:pre-wrap; word-wrap:break-word; }</style></head><body>%@</body></html>"

#import "DetailsViewController.h"
#import "Question.h"
#import "Downloader.h"
#import "PagingScrollViewController.h"


@interface DetailsViewController () {
    Question *question;
}
@property (nonatomic,retain) IBOutlet UILabel *reputation;
@property (nonatomic,retain) IBOutlet UILabel *gbadge;
@property (nonatomic,retain) IBOutlet UILabel *sbadge;
@property (nonatomic,retain) IBOutlet UILabel *bbdage;
@property (nonatomic,retain) IBOutlet UILabel *date;
@property (nonatomic,retain) IBOutlet UILabel *name;
@property (nonatomic, retain) IBOutlet UIWebView *wView;
@property (nonatomic, retain) IBOutlet UIButton *button;

-(IBAction)showAnswers;

@end

@implementation DetailsViewController

@synthesize question;
@synthesize name;
@synthesize date;
@synthesize reputation;
@synthesize gbadge;
@synthesize sbadge;
@synthesize bbdage;
@synthesize wView;
@synthesize button;

-(IBAction)showAnswers{
    PagingScrollViewController *paging = [[PagingScrollViewController new]autorelease];
    paging.answers = [Downloader getAnswersArray:question.answers];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.60];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.navigationController.view cache:NO];
    [self.navigationController pushViewController:paging animated:NO];
    [UIView commitAnimations];
}

-(void) dealloc;
{
    self.question = nil;
    self.name = nil;
    self.date = nil;
    self.reputation = nil;
    self.gbadge = nil;
    self.sbadge = nil;
    self.bbdage = nil;
    self.wView = nil;
    self.button = nil;
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
       
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.topViewController.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"fav_empt.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(insertNewObject)] autorelease];
    
    name.text = question.author;
    reputation.text =   question.reputation;
    date.text = question.date;
    gbadge.text = question.gbadge;
    sbadge.text = question.sbadge;
    bbdage.text = question.bbadge;
    if(question.answers!=nil){
        [button setTitle:[NSString stringWithFormat:question.answers.count==1 ? @"%@ answer" : @"%@ answers",question.answerCount] forState:UIControlStateNormal];
    }
    else button.Enabled = NO;
    wView.scrollView.bounces = NO;
    [wView loadHTMLString:[NSString stringWithFormat:scale,question.body] baseURL:nil];
    
}

-(void)viewWillAppear:(BOOL)animated{
    self.title = [NSString stringWithFormat:abs([question.votes intValue])==1 ? @"%@ vote" : @"%@ votes",question.votes];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
{
    
    NSURL *loadURL = [[request URL]retain];
    if(![[loadURL scheme] isEqualToString: @"http"])
    {
        [loadURL release];
        return YES;
    }
    [loadURL release];
    return NO;
}




- (void)insertNewObject
{
    NSManagedObjectContext *context = self.managedObjectContext;
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Question" inManagedObjectContext:self.managedObjectContext];
   Question *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name]
                                                                      inManagedObjectContext:context];
    
    [newManagedObject setValue:question.date forKey:@"date"];
    [newManagedObject setValue:question.questionText forKey:@"questionText"];
    [newManagedObject setValue:question.author forKey:@"author"];
    [newManagedObject setValue:question.votes forKey:@"votes"];
    [newManagedObject setValue:question.answerCount forKey:@"answerCount"];
    [newManagedObject setValue:question.reputation forKey:@"reputation"];
    [newManagedObject setValue:question.gbadge forKey:@"gbadge"];
    [newManagedObject setValue:question.sbadge forKey:@"sbadge"];
    [newManagedObject setValue:question.bbadge forKey:@"bbadge"];
    [newManagedObject setValue:question.body forKey:@"body"];
    [newManagedObject setValue:question.questionId forKey:@"questionID"];
    [newManagedObject setValue:question.image forKey:@"image"];
    /*NSArray *answerArray = [NSArray arrayWithArray:[Downloader getAnswersArray:question.answers]];
    for (int i = 0 ; i < [answerArray count] ; i++){
        
    Answer *answer = [NSEntityDescription  insertNewObjectForEntityForName:@"Answer"
                                                    inManagedObjectContext:self.managedObjectContext];
        answer = [answerArray objectAtIndex:i];
        NSLog(@"%@",answer);
    [newManagedObject addAnswersObject:answer];
    
    }*/
    [self saveContext];
    self.navigationController.topViewController.navigationItem.rightBarButtonItem.image = [UIImage imageNamed:@"fav.png"];
    self.navigationController.topViewController.navigationItem.rightBarButtonItem.enabled = NO;
}

- (void)saveContext {
    
    NSError *error = nil;
    if (![self.managedObjectContext save:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
}

@end
